package com.kuzufang.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.kuzufang.utils.DisplayUtils;

/**
 * Created by songyuanjin on 2017/9/27.
 */

public class CircularTextView extends View {
    private Paint mPaint;
    private float dx;
    private float dy;
    private int radius = 8;
    private Paint textPaint;
    private String number = "1";
    private static final String lineString = "号线";
    private int textSize = 14;

    public CircularTextView(Context context) {
        this(context, null);
    }

    public CircularTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircularTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.parseColor("#FF009688"));

        textPaint = new Paint();
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        textPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(DisplayUtils.sp2px(context, textSize));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(dx, dy, radius, mPaint);
        int yPos = (int) (dy - ((textPaint.descent() + textPaint.ascent()) / 2));
        String lineNum = number.substring(0, number.indexOf("号线"));

        textPaint.setTextSize(DisplayUtils.sp2px(this.getContext(), textSize / 2));
        int width = (int) textPaint.measureText(lineString);
        int smallTextDx = (int) (dx * 2 - width + dx / 8);
        canvas.drawText("号线", smallTextDx, yPos, textPaint);

        textPaint.setTextSize(DisplayUtils.sp2px(this.getContext(), textSize));
        int widthLine = (int) textPaint.measureText(lineNum);
        canvas.drawText(lineNum, dx * 2 - width - widthLine * 2 / 3 - dx / 8, yPos, textPaint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
        dx = getMeasuredWidth() / 2;
        dy = getMeasuredHeight() / 2;
    }

    private int measureWidth(int widthMeasureSpec) {
        int measureMode = MeasureSpec.getMode(widthMeasureSpec);
        int measureSize = MeasureSpec.getSize(widthMeasureSpec);
        int resultWidth = 30;
        if (measureMode == MeasureSpec.AT_MOST) {
            resultWidth = 30;
        } else if (measureMode == MeasureSpec.EXACTLY) {
            resultWidth = measureSize;
        }
        radius = resultWidth / 2;
        return resultWidth;
    }

    private int measureHeight(int heightMeasureSpec) {
        int measureMode = MeasureSpec.getMode(heightMeasureSpec);
        int measureSize = MeasureSpec.getSize(heightMeasureSpec);
        int resultHeight = 30;
        if (measureMode == MeasureSpec.AT_MOST) {
            resultHeight = 30;
        } else if (measureMode == MeasureSpec.EXACTLY) {
            resultHeight = measureSize;
        }
        return resultHeight;
    }

    public void setColorAndText(int color, String drawNum) {
        mPaint.setColor(color);
        number = drawNum;
        invalidate();
    }

    public int getTextSize() {
        return textSize;
    }

    public void setShowTextSize(int size) {
        textSize = size;
    }
}
